<!--
 * @author  dumeng
京东退款记录
-->
<template>
  <section class="jdactivity-account-main">
    <section class="main-search merge">
      <span class="svg-container">
        <svg-icon icon-class="search" />
      </span>
      <el-input
        v-model.trim="listQuery.refundNo"
        placeholder="输入退款号搜索"
        class="search-input"
        @keyup.enter.native="handleFilter"
      />
      <section class="search-btn">
        <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
        <el-button type="primary" @click="handleReset">重置</el-button>
      </section>
    </section>
    <section class="main-buttons">
      <section class="main-buttons-title">
        <span>京东退款记录</span>
      </section>
      <section class="main-buttons-btns">
        <!-- <el-button style="margin-left: 10px" @click="handleCreate">新建</el-button> -->
        <el-button
          style="padding: 10px 30px"
          type="primary"
          @click="handleCancel"
        >返回
        </el-button>
      </section>

    </section>
    <section class="main-table">
      <v-table :list="list" tag="refundList" :list-loading="listLoading" :filter-column="filtercolumn">
        <el-table-column label="操作" fixed="right" align="center" min-width="100" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <!--<el-button
                  type="text"
                  size="mini"
                  @click="handlePublish(scope.row)"
                  >{{ scope.row.status === "ZT01" ? "禁用" : "启用" }}</el-button
                > -->
            <!-- <el-button type="text" size="mini" @click="handleUpdate(scope.row)">编辑</el-button> -->
            <el-button
              v-if="scope.row.refundmentStatus!='90'"
              type="text"
              size="mini"
              @click="handleRefund(scope.row)"
            >退款
            </el-button>
          </template>
        </el-table-column>
      </v-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </section>
    <el-dialog
      v-el-drag-dialog
      :close-on-click-modal="false"
      :visible.sync="refundStatus"
      title="退款信息"
      center
      width="600px"
    >
      <p><strong :style="{color: '#333'}">下单时间：</strong>{{ obj.crtTime }}</p>
      <p><strong :style="{color: '#333'}">京东订单编号：</strong>{{ obj.orderNum }}</p>
      <p><strong :style="{color: '#333'}">订单金额：</strong>{{ obj.payMoney || 0 }}</p>
      <div>
        <strong :style="{color: '#333'}">付款信息：</strong>
        <p :style="{textIndent: '4em'}"><strong :style="{color: '#333'}">微信扣款：</strong>{{ obj.wxPayMoney || 0 }}</p>
        <p :style="{textIndent: '4em'}"><strong :style="{color: '#333'}">福利子账户：</strong>{{ obj.childAccountPayMoney || 0 }}</p>
        <div v-if="obj.ordinaryAccountPayMoneyList" class="other">
          <div class="left">
            <p style="margin-top:0;"><strong :style="{color: '#333'}">其他子账户：</strong> </p>
          </div>
          <div class="right">
            <p v-for="(item,i) in obj.ordinaryAccountPayMoneyList" :key="i" style="margin-top:0;">  {{ item.payMoney +"("+item.subAccountName || ''+"——"+item.subAccountType+")" }}</p>
          </div>
        </div>
      </div>
      <p><strong :style="{color: '#333'}">钱包付款流水号：</strong>{{ obj.runningNumber }}</p>
      <p><strong :style="{color: '#333'}">付款状态：</strong>{{ obj.orderStatus | filterStatus }}</p>
      <p><strong :style="{color: '#333'}">付款状态说明：</strong>{{ obj.errorReason || '无' }}</p>
      <p><strong :style="{color: '#333'}">可退款金额：</strong>{{ obj.refundMoney || 0 }}</p>
      <p v-show="!obj.refundMoney || obj.refundMoney==0 " :style="{color: 'red'}">*付款状态为{{ obj.orderStatus | filterStatus }}，无需退款。</p>
      <span slot="footer" class="dialog-footer">
        <el-button style="padding: 8px 30px;" type="primary" @click="refundStatus = false">取消</el-button>
        <el-button
          v-if="obj.orderStatus === '3'"
          style="padding: 8px 30px;"
          type="primary"
          @click="handleConfirmRefund()"
        >退款</el-button>
      </span>
    </el-dialog>
  </section>
</template>
<script>
import { del, queryPage, getDictByCode, refund } from '../api/refundList'
import Pagination from '@/components/Pagination'
import VTable from '@/components/Table'
import elDragDialog from '@/directive/el-drag-dialog'
import CryptoJS from '@/utils/aesJiaJM'

export default {
  name: 'JdActivity',
  components: { VTable, Pagination },
  directives: { elDragDialog },
  filters: {
    filterStatus(val) {
      switch (val) {
        case ('1'):
          return '待付款'
        case ('2'):
          return '付款中'
        case ('3'):
          return '付款成功'
        case ('4'):
          return '付款失败'
      }
    }
  },
  data() {
    return {
      list: [],
      listQuery: {
        page: 1,
        limit: 10,
        refundNo: ''
      },
      total: 0,
      listLoading: false,
      refundStatus: false,
      obj: {}
    }
  },
  watch: {},
  created() {
    this.getList()
  },
  methods: {
    getMoney(list) {
      const total = 0
      const sum = list.reduce((total, item) => total + item.payMoney, 0)
      return sum
    },
    formatTime(str) {
      return str && str.replace(/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/, '$1-$2-$3 $4:$5:$6')
    },

    getDict(code) {
      return new Promise((resolve, reject) => {
        getDictByCode(code).then(response => {
          this.dict[code] = response.data.data[0].list
        }).catch(error => {
          reject(error)
        })
      })
    },
    filtercolumn(scope, item) {
      if (item === 'rsaSignPrivateKey' || item === 'loginAccessKey' || item === 'phoneNumber') {
        return scope.row[item] ? CryptoJS.$aesJieM(scope.row[item]) : ''
      } else {
        return scope.row[item]
      }
    },
    handleCancel() {
      this.$router.push({ path: this.$filterPath('index'), query: { t: this.$route.query.t }})
    },
    handleReset() {
      this.listQuery.page = 1
      this.listQuery.refundNo = ''

      this.getList()
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    handleRefund(row) {
      this.obj = Object.assign({}, row, row.jdOrderVO)
      this.$set(this.obj, 'parentId', row.id)
      console.log('obj', this.obj)
      this.refundStatus = true
    },
    handleConfirmRefund() {
      this.$confirm('确定要退款吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: '请稍后',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.3)'
        })
        return new Promise((resolve, reject) => {
          refund({ id: this.obj.parentId }).then(() => {
            loading.close()
            this.$message({
              title: '成功',
              message: '退款成功',
              type: 'success',
              duration: 2000
            })
            this.refundStatus = false
            this.getList()
          }).catch(error => {
            loading.close()
            reject(error)
          })
        })
      }).catch(() => {
      })
    },
    handleDelete(ids) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: '请稍后',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.3)'
        })
        return new Promise((resolve, reject) => {
          del(ids).then(() => {
            loading.close()
            this.$message({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          }).catch(error => {
            loading.close()
            reject(error)
          })
        })
      }).catch(() => {
      })
    },
    getList(obj) {
      this.listLoading = true
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        giveId: this.$route.query.id,
        refundNo: this.listQuery.refundNo
      }
      return new Promise((resolve, reject) => {
        queryPage(query).then(response => {
          this.list = response.data.data.rows || []
          this.total = response.data.data.total
          this.listLoading = false
        }).catch(error => {
          reject(error)
          this.listLoading = false
        })
      })
    }
  }
}
</script>
<style lang="scss">

.jdactivity-account-main {
  padding: 10px;
  .other{
    display: flex;
    flex-direction: row;
    .left{
      width: 140px;
      text-align: right;
    }
    .right{
      flex: 1;
    }
  }
  .el-dialog .el-dialog__body {
    max-height: 73vh !important;
  }
  .line {
    text-align: center;
  }
  .main-search {
    background-color: #fff;
    padding: 0 10px;
    .svg-container {
      padding: 0 5px 0 10px;
      color: #bfced6;
      vertical-align: middle;
      display: inline-block;
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }
  }

  .main-buttons {
    display: flex;
    line-height: 36px;
    margin-bottom: 10px;
    .main-buttons-title {
      flex: 1;
      height: 36px;
      line-height: 36px;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
      .num {
        @include font-color($mainColor);
      }
    }
  }
  .el-dialog .el-dialog__footer {
    padding: 10px !important;
  }
}
</style>
